package com.example.springcloud.controller;


import com.example.springcloud.common.ResponseEntity;
import com.example.springcloud.entity.Dept;
import com.example.springcloud.service.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 莫磊
 * @date 2025/8/20
 * @description
 */
@RestController
@RequestMapping("/dept")
@Tag(name = "部门接口", description = "提供部门的数据操作接口")
public class DeptController {

    @Resource
    private DeptService service;

    @GetMapping("/getById/{id}")
    @Operation(summary = "根据id查询部门", description = "返回指定id的部门", responses = {
            @ApiResponse(
                description = "成功获取到部门信息", responseCode = "200",
                    content = @Content(schema = @Schema(implementation = Dept.class))
            )
    }
    )
    public ResponseEntity<Dept> getById(@PathVariable("id") Long id) {
        Dept dept = (Dept) service.getById(id);
        Assert.notNull(dept, "部门不存在");
        return ResponseEntity.success(dept);
    }

    @PostMapping("/addDept")
    @Operation(summary = "添加部门")
    public ResponseEntity<Dept> addDept(Dept dept) {
        int i = service.addDept(dept);
        Assert.isTrue(i > 0, "添加部门失败");
        return ResponseEntity.success(dept);
    }

    @Operation(summary = "根据部门名称模糊查询部门",
            parameters = @Parameter(name = "dname", description = "部门名称"))
    @GetMapping("/findByDnameLike/{dname}")
    public ResponseEntity<List<Dept>> findByDnameLike(@PathVariable("dname") String dname) {
        return ResponseEntity.success( (List) service.findByDnameLike(dname));
    }

    @GetMapping("/findByDeptno/{deptno}")
    @Operation(summary = "根据部门编号查询部门",
            parameters = @Parameter(name = "deptno", description = "部门编号"))
    public ResponseEntity<List<Dept>> findByDeptno(@PathVariable("deptno") String deptno) throws InterruptedException {
        Thread.sleep(100000);
        return ResponseEntity.success( (List) service.findByDeptno(deptno));
    }

}
